<?php
include_once "settings.php";
include_once "messages.php";

function show_page() {
  global $data, $path, $style, $session_id, $header;

  if (!isset($data["type"])) {
    header("Location: index.php?trg=main&sid=".$session_id);
    die();
  }

  $filename = $path[$style]["tpl"]."/main.htm";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = "";

  $i = 0;
  $module[$i]["name"] = getMessage("Administrator");
  $module[$i]["user"] = 2; //at least admin
  $module[$i]["img"] = "admin.png";
  $module[$i]["trg"] = "admin";
  $module[$i]["status"] = 1; //ok
  $i++;

  $module[$i]["name"] = getMessage("My settings");
  $module[$i]["user"] = 1; //at least user
  $module[$i]["img"] = "my.png";
  $module[$i]["trg"] = "user";
  $module[$i]["status"] = 1; //ok
  $i++;

  $module[$i]["name"] = getMessage("Ontology editor");
  $module[$i]["user"] = 0; //at least guest
  $module[$i]["img"] = "editor.png";
  $module[$i]["trg"] = "ontoedit";
  $module[$i]["status"] = 1; //ok
  $i++;

  $module[$i]["name"] = getMessage("Ontology merger");
  $module[$i]["user"] = 0; //at least guest
  $module[$i]["img"] = "merge.png";
//  $module[$i]["trg"] = "drawmerg";
  $module[$i]["trg"] = "merger_choise";//"drawmerg";
  $module[$i]["status"] = 1; //not available
  $i++;

  $module[$i]["name"] = getMessage("Ontology importer/exporter");
  $module[$i]["user"] = 0; //at least guest
  $module[$i]["img"] = "export.png";
  $module[$i]["trg"] = "importexport";
  $module[$i]["status"] = 1; //not available
  $i++;

  $module[$i]["name"] = getMessage("Term searcher");
  $module[$i]["user"] = 0; //at least guest
  $module[$i]["img"] = "search.png";
  $module[$i]["trg"] = "termsearcher";
  $module[$i]["status"] = 1; //not available
  $i++;

  $module[$i]["name"] = "Ontology Translator";
  $module[$i]["user"] = 0; //at least guest
  $module[$i]["img"] = "translate.png";
  $module[$i]["trg"] = "";
  $module[$i]["status"] = 0; //not available
  $i++;

  $rep_cont = "";
  foreach ($module as $mdl) {
    if ($data["type"] >= $mdl["user"]) {
      $tmp_cont = str_replace("%MDL_NAME%", $mdl["name"], $matches[1]);
      $tmp_cont = str_replace("%MDL_IMG%", $mdl["img"], $tmp_cont);
      if ($mdl["status"] == 1) {
        $tmp_cont = str_replace("%MDL_LINK%", "index.php?trg=".$mdl["trg"]."&sid=%SID%", $tmp_cont);
        $tmp_cont = str_replace("%STYLE%", "", $tmp_cont);
      } else {
        $tmp_cont = str_replace("%MDL_LINK%", "javascript:;", $tmp_cont);
        $tmp_cont = str_replace("%STYLE%", "grey", $tmp_cont);
      }
      $rep_cont .= $tmp_cont;
    }
  }
  $contents = str_replace($matches[0], $rep_cont, $contents);

  $contents = str_replace("%ROOT_CSS%", $path[$style]["css"], $contents);
  $contents = str_replace("%ROOT_IMG%", $path[$style]["img"], $contents);
  $contents = str_replace("%LOGOUT%", getMessage("Logout"), $contents);
  $contents = str_replace("%MAINMENU%", getMessage("Main menu"), $contents);
  $contents = str_replace("%SID%", $session_id, $contents);

  header ($header["html"]);
  header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
  header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  header ("Pragma: no-cache");                          // HTTP/1.0

        
  print ($contents);
}
?>